#! -*- coding: utf-8 -*-
import json

from script_utils import query, oid2nid
from script_utils import models

cols = {
    "mtu_cols": [
        "id",
        "target_type",
        "target_id",
        "destination_id",
        "category_id",
        "published",
    ],
    "mtucomment_cols": [
        "id",
        "mtu_id",
        "user_id",
        "text",
        "is_active",
        "liked_count",
        "score",
        "last_modified",
        "date_added",
    ],
    "mtudialog_cols": [
        "id",
        "mtu_id",
        "user_id",
        "text",
        "is_active",
        "last_modified",
        "date_added",
    ],
    "poi_cols": [
        "id",
        "name",
        'st_asgeojson(st_astext(location))',
    ]
}
poi_migrate = False


def migrate_poi():
    global poi_migrate
    for row in query('poi_poi', cols=cols['poi_cols']):
        obj = dict(zip(cols['poi_cols'], row))
        location_field = 'st_asgeojson(st_astext(location))'
        obj['location'] = obj[location_field] \
                          and json.loads(obj[location_field])["coordinates"]
        del obj[location_field]
        obj['target_id'] = obj['id']
        del obj['id']

        models.Poi.objects.create(**obj)

    poi_migrate = True


def migrate_mtu():
    destination = oid2nid["vacation_destination"]
    product = oid2nid["vacation_product"]
    for row in query('vacation_mtu', cols=cols['mtu_cols']):
        obj = dict(zip(cols['mtu_cols'], row))

        oid = obj['id']
        obj['id'] = oid2nid["vacation_mtu"][oid]

        obj['destination'] = destination[obj['destination_id']]
        del obj['destination_id']

        if obj['target_type'] == 2:
            obj['target_id'] = str(product[obj['target_id']])
        else:
            obj['target_id'] = str(obj['target_id'])
        models.Mtu.objects.create(**obj)

    if not poi_migrate:
        raise Exception("run migrate_poi func first")

    poi_mtus = models.Mtu.objects.filter(target_type=1)
    for e in poi_mtus:
        poi = models.Poi.objects.filter(target_id=e.target_id).first()
        if poi:
            e.poi_name = poi.name
            e.location = poi.location
            e.save()


def migrate_mtucomment():
    for row in query('vacation_mtucomment', cols=cols['mtucomment_cols']):
        obj = dict(zip(cols['mtucomment_cols'], row))
        oid = obj['id']
        obj['id'] = oid2nid["vacation_mtucomment"][oid]
        models.MtuComment.objects.create(**obj)


def migrate_mtudialog():
    mtu = oid2nid["vacation_mtu"]
    for row in query('vacation_poidialog', cols=cols['mtudialog_cols']):
        obj = dict(zip(cols['mtudialog_cols'], row))
        print obj
        oid = obj['id']
        obj['id'] = oid2nid["vacation_poidialog"][oid]

        obj['mtu'] = mtu[obj['mtu_id']]
        del obj['mtu_id']

        models.MtuDialog.objects.create(**obj)


if __name__ == "__main__":
    print "migrate poi start..."
    migrate_poi()
    print "migrate poi end..."

    print 'migrate vacation_mtu start...'
    migrate_mtu()
    print 'migrate vacation_mtu end...'

    print 'migrate vacation_mtucomment start...'
    migrate_mtucomment()
    print 'migrate vacation_mtucomment end...'

    print 'migrate vacation_mtudialog start...'
    migrate_mtudialog()
    print 'migrate vacation_mtudialog end...'
